package com.anyou.mall.fin.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;

import com.anyou.mall.dri.domain.Driver;
import com.anyou.mall.dri.mapper.DriverMapper;
import com.anyou.mall.fin.param.DisableParam;
import com.anyou.mall.fin.vo.AllBondVo;
import com.anyou.mall.fin.vo.BondNumVo;
import com.anyou.mall.fin.vo.BondVo;
import com.anyou.mall.platform.domain.PlaPlatformSettings;
import com.anyou.mall.platform.mapper.PlaPlatformSettingsMapper;
import com.anyou.mall.platform.service.IPlaPlatformSettingsService;
import com.anyou.mall.platform.vo.BondVO;
import com.anyou.mall.sho.domain.CarPartsShop;
import com.anyou.mall.sho.mapper.CarPartsShopMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import org.apache.xmlbeans.impl.xb.xsdschema.All;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.fin.mapper.BondMapper;
import com.anyou.mall.fin.domain.Bond;
import com.anyou.mall.fin.service.IBondService;

/**
 * 保证金管理Service业务层处理
 *
 * @author gaochaohao
 * @date 2022-04-19
 */
@Service
public class BondServiceImpl implements IBondService
{
    @Autowired
    private BondMapper bondMapper;
    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private PlaPlatformSettingsMapper plaPlatformSettingsMapper;

    @Autowired
    private CarPartsShopMapper carPartsShopMapper;



    /**
     * 查询保证金管理
     *
     * @param bondId 保证金管理主键
     * @return 保证金管理
     */
    @Override
    public Bond selectBondByBondId(Long bondId)
    {
        return bondMapper.selectBondByBondId(bondId);
    }

    /**
     * 查询保证金管理列表
     *
     * @param bond 保证金管理
     * @return 保证金管理
     */
    @Override
    public List<Bond> selectBondList(Bond bond)
    {
        List<Bond> bondList = bondMapper.selectBondList(bond);


        return bondList;
    }

    /**
     * 新增保证金管理
     *
     * @param bond 保证金管理
     * @return 结果
     */
    @Override
    public int insertBond(Bond bond)
    {
        bond.setCreateTime(DateUtils.getNowDate());
        return bondMapper.insertBond(bond);
    }

    /**
     * 修改保证金管理
     *
     * @param bond 保证金管理
     * @return 结果
     */
    @Override
    public int updateBond(Bond bond)
    {
        bond.setUpdateTime(DateUtils.getNowDate());
        return bondMapper.updateBond(bond);
    }

    /**
     * 批量删除保证金管理
     *
     * @param bondIds 需要删除的保证金管理主键
     * @return 结果
     */
    @Override
    public int deleteBondByBondIds(Long[] bondIds)
    {
        return bondMapper.deleteBondByBondIds(bondIds);
    }

    /**
     * 删除保证金管理信息
     *
     * @param bondId 保证金管理主键
     * @return 结果
     */
    @Override
    public int deleteBondByBondId(Long bondId)
    {
        return bondMapper.deleteBondByBondId(bondId);
    }
    /**
     * 启用禁用接口
     * @param disableParam
     * @return
     */
    @Override
    public ApiResult disable(DisableParam disableParam) {

        Bond bond = bondMapper.selectBondByBondId(disableParam.getBondId());
        if ("0".equals(bond.getBondType())) {
            CarPartsShop carPartsShop = carPartsShopMapper.selectById(bond.getPartnerId());
            carPartsShop.setEnableStatus(disableParam.getEnableStatus());
            return ApiResult.success(carPartsShopMapper.updateCarPartsShop(carPartsShop));
        } else if ("2".equals(bond.getBondType())) {
            // 修改司机表中的状态

            return ApiResult.success();
        } else {
            //维修厂还未弄，暂时不判断
            return ApiResult.success();
        }
    }
    /**
     * 获取保证金总额
     * @return
     */
    @Override
    public ApiResult<AllBondVo> allBond() {
        AllBondVo allBondVo = new AllBondVo();

        //查询合伙人数量与总保证金额
        BondNumVo bnc=carPartsShopMapper.selectTotalBond();
        if(bnc==null){
            allBondVo.setTotalShop(0);
            allBondVo.setTotalShopBond(new BigDecimal(0));
        }else{
            allBondVo.setTotalShop(bnc.getTotalNum());
            allBondVo.setTotalShopBond(bnc.getTotalMoney());
        }

        //查询司机数量与总保证金额
        BondNumVo bnd=driverMapper.selectTotalBond();
        if(bnd==null){
            allBondVo.setTotalDriver(0);
            allBondVo.setTotalDriverBond(new BigDecimal(0));
        }else{
            allBondVo.setTotalDriver(bnd.getTotalNum());
            allBondVo.setTotalDriverBond(bnd.getTotalMoney());
        }


        return ApiResult.success(allBondVo);
    }
    /**
     * 查询合伙人列表
     * @param bond
     * @return
     */
    @Override
    public List<Bond> selectPartnerBondList(Bond bond) {

        return bondMapper.selectPartnerBondList(bond);
    }

    /**
     * 查询司机列表
     * @param bond
     * @return
     */
    @Override
    public List<Bond> selectDriverBondList(Bond bond) {
        return bondMapper.selectDriverBondList(bond);
    }
}
